package esmp.jtt808;

import esmp.jtt808.command.Jtt808Cmd;
import esmp.jtt808.model.DataPack;
import esmp.jtt808.model.platform.PlatCommonResp;
import esmp.jtt808.model.platform.TerminalRegisterResp;
import esmp.jtt808.model.terminal.TerminalCommonResp;
import esmp.jtt808.model.terminal.TerminalParamQueryResp;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import netty.session.NettySession;
import netty.session.SessionManager;
import netty.task.NettyDeferredTaskManager;

import java.util.UUID;

/**
 * Created by dave on 18-7-20 下午4:26.
 */
public class MessageProcessor {
    private NettyDeferredTaskManager taskManager;
    private SessionManager sessionManager;

    public MessageProcessor(SessionManager sessionManager, NettyDeferredTaskManager taskManager) {
        this.sessionManager = sessionManager;
        this.taskManager = taskManager;
    }

    public void process(NettySession session, DataPack<?> dp) {
        switch (dp.getMsgId()) {
            case 0x0001: { // 终端通用应答消息体
                TerminalCommonResp resp = (TerminalCommonResp) dp.getBody();
                Jtt808Cmd cmd = Jtt808Cmd.of(resp.getPlatMsgId());
                if (cmd != null) {
                    taskManager.receivedResult(dp.getSim(), JTT808CmdExecutor.TASK_PREFIX + cmd.name(), resp);
                }
                break;
            }
            case 0x0002: { // 终端心跳
                succeed(session.getChannel(), dp);
                break;
            }
            case 0x0100: { // 终端注册
                TerminalRegisterResp res = new TerminalRegisterResp();
                res.setTerminalSequenceNo(dp.getSequenceNo());
                res.setResult(0);
                res.setAuth(UUID.randomUUID().toString());
                session.getChannel().writeAndFlush(Unpooled.wrappedBuffer(res.encode()));
                break;
            }
            case 0x0003: { // 终端注销
                succeed(session.getChannel(), dp);
                break;
            }
            case 0x0102: { // 终端鉴权
                succeed(session.getChannel(), dp);
                break;
            }
            case 0x0104: { // 查询终端参数应答
                TerminalParamQueryResp resp = (TerminalParamQueryResp) dp.getBody();
                taskManager.receivedResult(dp.getSim(), JTT808CmdExecutor.TASK_PREFIX + Jtt808Cmd.setParam.name(), resp);
                break;
            }
            case 0x0200: { // 位置信息上报
                succeed(session.getChannel(), dp);
                break;
            }
        }
    }

    private void succeed(Channel channel, DataPack<?> dp) {
        DataPack<PlatCommonResp> re = new DataPack<>();
        re.setMsgId(0x8001);
        re.setSim(dp.getSim());
        re.setSequenceNo(sessionManager.getSession(dp.getSim()).nextSequenceNo());

        PlatCommonResp body = new PlatCommonResp();
        body.setTerminalSequenceNo(dp.getSequenceNo());
        body.setTerminalMsgId(dp.getMsgId());
        body.setResult(DataPack.success);
        re.setBody(body);
        channel.writeAndFlush(Unpooled.wrappedBuffer(re.encode()));
    }
}
